package br.com.searchpeople.adapter;

import java.util.List;

import br.com.searchpeople.R;
import br.com.searchpeople.database.SingletonAccountBD;
import br.com.searchpeople.model.PostModel;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PostAdapter extends BaseAdapter {

	private List<PostModel> posts_item;
	private LayoutInflater mInflater;
	private ViewHolder holder;
	

	static class ViewHolder {
		private TextView textview_name;
		private TextView textview_place;
		private ImageView imageview_post;
		private TextView textview_nickname;
	}

	public PostAdapter(Context context, List<PostModel> posts_item) {
		mInflater = LayoutInflater.from(context);
		this.posts_item = posts_item;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return posts_item.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return posts_item.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SharedPreferences settings = parent.getContext().getSharedPreferences("general", Context.MODE_PRIVATE);
		String login = settings.getString("login", "");
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.activity_post_adapter_item, null);
			holder = new ViewHolder();

			holder.textview_name = (TextView) convertView
					.findViewById(R.id.textview_adapter_name);
			holder.textview_place = (TextView) convertView
					.findViewById(R.id.textview_adapter_place);
			holder.imageview_post = (ImageView) convertView
					.findViewById(R.id.imageview_adapter_post);
			holder.textview_nickname = (TextView) convertView.findViewById(R.id.textview_adapter_nickname);
			
			convertView.setTag(holder);

			
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		PostModel post = posts_item.get(position);
		Bitmap image = resizeBitmap(post.getImg(), 300, 300);
		String nickname = SingletonAccountBD.getInstance().getAccountByID(convertView.getContext(), post.getId_user()).getUser_nickname();

		holder.textview_name.setText(post.getName());
		holder.textview_place.setText(post.getCountry() + " - "
				+ post.getCity());
		holder.imageview_post.setImageBitmap(image);
		//holder.textview_nickname.setText("Postado por: " + nickname);
		holder.textview_nickname.setText("Postado por: " + post.getId_user());
		return convertView;
	}

	private Bitmap resizeBitmap(Bitmap bp, int newHeight, int newWidth) {

		float scaleHeight = ((float) newHeight) / bp.getHeight();
		float scaleWidth = ((float) newWidth) / bp.getWidth();

		Matrix matrix = new Matrix();

		matrix.postScale(scaleWidth, scaleHeight);

		Bitmap resizedBitmap = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(),
				bp.getHeight(), matrix, true);
		return resizedBitmap;
	}

}
